package com.rd.student.dashuju.zhengchengxiang.Day0731;


import java.util.Arrays;

public class RandomTest {
    public static void main(String[] args) {
        // 排序前
        System.out.println("排序前：");
        int[][] arr2 = random1(50, 20, 10);
        // 排序后
        System.out.println("排序后：");
        sort1(arr2);

    }


    // range: 随即范围  number: 一次随机的数量   frequency: 随机次数
    public static int[][] random1(int range, int number, int frequency) {
        int[] arr = new int[range];
        int[] arr1 = new int[number];
        int[][] arr2 = new int[frequency][number];

        // 将原数组arr全部赋值为-1
        for (int i = 0; i < arr.length; i++) {
            arr[i] = -1;
        }

        for (int j = 0; j < frequency; j++) {
            // 将随机出来的20个随机数放入arr1
            for (int i = 0; i < number; i++) {
                // 生成随机数
                int digit = (int) (Math.random() * range);
                // 没重复就添加
                if (arr[digit] == -1) {
                    arr1[i] = digit;
                } else {
                    i--;
                }
            }
            // 将arr1放入arr2中
            for (int i = 0; i < arr1.length; i++) {
                arr2[j][i] = arr1[i];
            }
        }

        // 遍历arr2
        // 随机一次放入第一行，第二行，以此类推······
        for (int i = 0; i < frequency; i++) {
            for (int i1 = 0; i1 < number; i1++) {
                System.out.printf("%-3d", arr2[i][i1]);
            }
            System.out.println();
        }
        return arr2;
    }

    public static void sort1(int[][] arr2) {
        // 行数
        int frequency = arr2.length;
        // 列数
        int number = arr2[0].length;
        // 遍历arr2
        // 随机一次放入第一行，第二行，以此类推······
        for (int i = 0; i < frequency; i++) {
            // 创建arr3
            int[] arr3 = new int[number];
            for (int i1 = 0; i1 < number; i1++) {
                //把arr2的每一行放入arr3
                arr3[i1] = arr2[i][i1];
                if (i1 == number - 1) {
                    // 排序
                    Arrays.sort(arr3);
                    for (int i2 = 0; i2 < arr3.length; i2++) {
                        System.out.printf("%-3d", arr3[i2]);
                    }
                }

            }
            System.out.println();
        }

    }
}
